package zhoufu.sz.sanxiao_check.ui.adapter.items;

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import eu.davidea.flexibleadapter.FlexibleAdapter;
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.databinding.ItemShowpicBinding;
import zhoufu.sz.sanxiao_check.models.DangerModel;
import zhoufu.sz.sanxiao_check.ui.adapter.FireSubItemAdapter;
import zhoufu.sz.sanxiao_check.weiget.ImageWithDelView;

/**
 * Created by hyc on 16-8-4.
 * 项目 ： XTSanXiao_V2
 * 开发 ： hyc
 * 时间 ： 16-8-4 下午2:55
 * 功能 ： TODO
 */
public class ShowPicsItem extends DataBindingItem<DangerModel.BeforeImgsEntity> implements ImageWithDelView.ImageOnclickListener {

    private FlexibleAdapter.OnItemClickListener _itemClickListener;
    private int _position;
    /**
     * @see FireSubItemAdapter
     */
    private FireSubItemAdapter _adapter;
    private ViewHolder _viewHolder;
    private ItemShowpicBinding _showpicBinding;


    @Override
    public boolean equals(Object o) {

        if (o instanceof ShowPicsItem) {
            ShowPicsItem item = (ShowPicsItem) o;
            return TextUtils.equals(item.getDB().getImg(), this.getDB().getImg());
        }

        return false;

    }

    @Override
    protected void setVariable(FlexibleAdapter adapter, ViewHolder holder, int position, List payloads) {
        super.setVariable(adapter, holder, position, payloads);
        _viewHolder = holder;
        _itemClickListener = adapter.mItemClickListener;
        _position = holder.getFlexibleAdapterPosition();
        _adapter = (FireSubItemAdapter) adapter;
        _showpicBinding = (ItemShowpicBinding) holder._dataBinding;
        _showpicBinding.showpicIwd.setImageOnclickListener(this);
        _showpicBinding.showpicIwd.setTag(_position);

        _showpicBinding.tvShowpic.setOnClickListener(_tvClickListener);
        /**
         *
         * @see  _showpicBinding.showpicIwd.clearPic()
         * 清除操作，避免图片显示错误
         *
         */
        if (null == getDB()||TextUtils.isEmpty(getDB().getImg())) {
            _showpicBinding.showpicIwd.clearPic();
            _showpicBinding.tvShowpic.setVisibility(View.GONE);
        } else {
            _showpicBinding.setEntity(getDB());
            _showpicBinding.tvShowpic.setVisibility(View.VISIBLE);
        }

    }


    @Override
    public void imageClick(ImageWithDelView view) {
        _itemClickListener.onItemClick(_viewHolder, _adapter, _position);
    }

    /**
     * 打开EditFragment 输入评论
     */
    public void updateDescri(@NonNull String text) {

        getDB().setDescribe(text);
        _showpicBinding.tvShowpic.setText(text);
    }

    private View.OnClickListener _tvClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Object fragment = _adapter.getFragment();
            if ((fragment instanceof OnAddDescribeListener) && null != getDB()) {
                OnAddDescribeListener obj = (OnAddDescribeListener) fragment;
                obj.clickAddDescriber(ShowPicsItem.this, _position);
            }
        }
    };

    public interface OnAddDescribeListener {
        void clickAddDescriber(ShowPicsItem item, int position);
    }

    @Override
    public int getLayoutRes() {
        return R.layout.item_showpic;
    }

    @Override
    public void imageDelClick(ImageWithDelView delView) {
        /**
         * 删除当前的图片,并更新数据到集合
         */
        int tag = (int) delView.getTag();

        _adapter.removeItem(tag);

        _showpicBinding.tvShowpic.setVisibility(View.GONE);

    }

}
